/**
 * Copyright (C) 2011 - @le.nguyen
 * @Created Jul 27, 2011
 *
 * TODO
 */
package com.conan.app.dailyflickr.app;

import java.util.HashMap;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;

import com.conan.app.dailyflickr.activity.DowloadedPhotosActivity;

/**  */
public class ProgressNotification {
    private static ProgressNotification instance;
    private static Context mContext;
    HashMap<String,NotifyItem > mNotificaitons = new HashMap<String, ProgressNotification.NotifyItem>();
    private NotificationManager mNotificationManager;

    public class NotifyItem{
        private int id;
        private String title;
        public boolean isWallpaper = false;
        // public NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this);
        public NotificationCompat.Builder _notification = null;
        public NotifyItem(String strTitle, boolean isWallpaper) {
            id = (int)System.currentTimeMillis();
            this.isWallpaper = isWallpaper;
            if(!TextUtils.isEmpty(strTitle)){
                if(strTitle.length() > 40){
                    title = strTitle.substring(0,40).concat("...");
                }else{
                    title = strTitle;
                }
            }
        }
    }

    public static ProgressNotification getInstance(Context context){
        mContext = context;
        if(instance == null){
            instance = new ProgressNotification();
            instance.mNotificationManager = (NotificationManager)
                    mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return instance;
    }


    public boolean hasDownloading(){
        return mNotificaitons.size() > 0 ? true : false;
    }
    
    public void showNotification(String url, String title, boolean isWallpaper){
        NotifyItem notify = new NotifyItem(title, isWallpaper);

        if(title.length() > 50)
            title = title.substring(0,50);
        // configure the notification
        if(notify._notification == null){
            notify._notification = new NotificationCompat.Builder(mContext) ;
        }
        
        Intent intent = new Intent(mContext,  DowloadedPhotosActivity.class);
        final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
        notify._notification.setContentIntent(pendingIntent);
        notify._notification.setSmallIcon(android.R.drawable.stat_sys_download);
        notify._notification.setTicker(notify.title);
        notify._notification.setContentTitle(notify.title);
        notify._notification.setContentInfo(notify.isWallpaper ? "Downloading for wallpaper" : "Downloading");
        
        try{
            notify._notification.setLargeIcon(
                BitmapFactory.decodeResource(mContext.getResources(), android.R.drawable.ic_popup_sync));
        } catch (OutOfMemoryError e) {
            Log.e("TAG", "decode resource notificaiton falied cause OOM ...");
        }
        notify._notification.setAutoCancel(false);
        mNotificationManager.notify(notify.id, notify._notification.getNotification());
        mNotificaitons.put(url, notify);
    }

    public void updateNotificationDone(String url, boolean success){
        NotifyItem notify  = mNotificaitons.remove(url);
        if(notify == null) return;

        Intent intent = new Intent(mContext,  DowloadedPhotosActivity.class);
        final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
        notify._notification.setContentIntent(pendingIntent);
        notify._notification.setTicker(notify.title);
        notify._notification.setContentTitle(notify.title);
        notify._notification.setAutoCancel(true);
        notify._notification.setLargeIcon(null);        
        String title = notify.isWallpaper ? "Set wallpaper" : "Download";
        notify._notification.setContentInfo("");
        if(success){
            notify._notification.setSmallIcon(android.R.drawable.stat_sys_download_done);
            notify._notification.setContentText(title + " success !!!");
        }else{
            notify._notification.setSmallIcon(android.R.drawable.stat_sys_download_done);
            notify._notification.setContentText(title + " failed !!!");
        }
        
        // notify._notification.set
        notify._notification.setAutoCancel(true);
        mNotificationManager.notify(notify.id, notify._notification.getNotification());
    }

    public void deleteAllNotifications(){
        final NotificationManager notificationManager = (NotificationManager)
                mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        NotifyItem notify;
        for(String url : mNotificaitons.keySet()){
            notify  = mNotificaitons.get(url);
            notificationManager.cancel(notify.id);
        }

        mNotificaitons.clear();
    }

    public void deleteNotification(String url){
        NotifyItem notify  = mNotificaitons.remove(url);
        final NotificationManager notificationManager = (NotificationManager)
                mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(notify.id);
    }
}
